﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using super_scrable.Models.Exceptions;
using super_scrable.Views.Widgets;

namespace super_scrable.Models
{
    public class Case : LetterZone
    {      
        /*
         * Couleur code :
         *  Blanc : x1
         *  Bleu clair : lettre x2
         *  Bleu foncé : lettre x3
         *  Rose : Mot x2
         *  Rouge : Mot x3
         */
        public const int BLANC = 0;
        public const int BLEU_CLAIR = 1;
        public const int BLEU_FONCE = 2;
        public const int ROSE = 3;
        public const int ROUGE = 4;

        private int x;
        private char y;
        private int color;
        private Letter letter=null;
   
        public int getX(){ return this.x; }
        public int getY() { return this.y; }
        public int getColor() { return this.color; }
        public Letter getLetter() { return this.letter; }
        public void setLetter(Letter letter)
        {
            if (this.letter != null)
                throw new AlreadyFillException("Case have already a letter");
            this.letter = letter;
            //this.Text = Convert.ToString(letter);
            //props graphique
            this.Value = letter.Point;
            this.Letter = letter.Character;
            this.resizeLabel();
        }

        public void setLetter(char letter,int point)
        {
            if (this.letter != null)
                throw new AlreadyFillException("Case have already a letter");
            this.letter = new Letter(point,letter);
            //this.Text = Convert.ToString(letter);

            //props graphique
            this.Value = point;
            this.Letter = letter;
            this.resizeLabel();
        }

        public void DeleteLetter()
        {
            this.letter = null;
        }

        public Case(int x, char y, int color)
        {
            this.x = x;
            this.y = y;
            this.color = color;
        }

        public Case(int x, int y, int color)
        {
            char[] header_y = { ' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O' };
            this.x = x;
            this.y = header_y[y];
            this.color = color;
        }

        public Case(int color)
        {
            this.color = color;
        }

        public string toString(){
            string color="";
            switch (this.color)
            {
            case Case.BLANC:
                color = "white";
                break;
            case Case.BLEU_CLAIR:
                color = "light blue";
                break;
            case Case.BLEU_FONCE:
                color = "dark blue";
                break;
            case Case.ROSE:
                color = "pink";
                break;
            case Case.ROUGE:
                color = "red";
                break;
            }

            return "x:" + x + ", y:" + y + ", color:" + color;
        }

        public Boolean IsNextTo(Case c)
        {
            // case en diagonale
            if (this.x != c.x && this.y != c.y) return false;

            // case collée
            if (this.x == c.x)
            {
                if (this.y == c.y - 1) return true;
                if (this.y == c.y + 1) return true;
            }
            if (this.y == c.y)
            {
                if (this.x == c.x - 1) return true;
                if (this.x == c.x + 1) return true;
            }

            /*if (this.x == c.x - 1) return true;
            if (this.x == c.x + 1) return true;
            if (this.y == c.y) return true;
            if (this.y == c.y - 1) return true;
            if (this.y == c.y + 1) return true;*/
            

            return false;
        }

        public int GetDirection(Case c)
        {
            if (this.x == c.x)
            {
                if (this.y == c.y - 1) return Direction.SOUTH;
                if (this.y == c.y + 1) return Direction.NORTH;
            }
            if (this.y == c.y)
            {
                if (this.x == c.x - 1) return Direction.OUEST;
                if (this.x == c.x + 1) return Direction.EST;
            }

            throw new InvalidDirectionException();
        }

        public int CompareTo(object o)
        {

            if (o == null) return 1;

            Case c = o as Case;

            if (this.x == c.x && this.y == c.y) return 0;
            if (this.x <= c.x && this.y <= c.y) return -1;
            return 1;
        }
    }
}
